<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Pull\Center;

define('TOKEN',"aniuaj");
class CuspussageController extends Controller
{

    public function __construct()
    {
    }

    /**
     * Notes: 检查服务地址URL
     * User: aniu
     * Date: 2020/6/17
     * Time: 10:07
     */
    public function check_server()
    {
        if (isset($_GET['echostr'])) {			//校验服务器地址URL
            $this->valid();
        }else{
            $this->responseMsg();
        }
    }

    public function valid()
    {
        $echoStr = $_GET["echostr"];
        if($this->checkSignature()){
            header('content-type:text');
            echo $echoStr;
            exit;
        }else{
            echo $echoStr.'+++'.TOKEN;
            exit;
        }
    }

    /**
     * Notes:
     * User: aniu
     * Date: 2020/6/17
     * Time: 10:09
     * @return bool
     */
    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr, SORT_STRING);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }

    /**
     * Notes: 推送消息
     * User: aniu
     * Date: 2020/6/17
     * Time: 10:16
     */
    public function responseMsg()
    {
        $postStr = file_get_contents('php://input');   //此处推荐使用file_get_contents('php://input')获取后台post过来的数据

        if (!empty($postStr) && is_string($postStr)){
            $postArr = json_decode($postStr,true);
            if(!empty($postArr['MsgType']) && $postArr['Content'] == "2"){			//用户发送1,回复公众号二维码
                $fromUsername = $postArr['FromUserName'];   						//发送者openid
                $imgurl = "/WechatIMG1.jpeg";											//公众号二维码,相对路径,修改为自己的
                $media_id = $this->getMediaId($imgurl);   									//获取图片消息的media_id
                $data=array(
                    "touser"=>$fromUsername,
                    "msgtype"=>"image",
                    "image"=>array("media_id"=>$media_id)
                );
                $json = json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+
                $this->requestAPI($json);
            }

            elseif(!empty($postArr['MsgType']) && $postArr['Content'] == "1"){		//用户发送2,回复图文链接
                $fromUsername = $postArr['FromUserName'];   						//发送者openid
                $data=array(
                    "touser"=>$fromUsername,
                    "msgtype"=>"link",
                    "link"=>array(											//修改下面几项为自己的
                        "title"=>'下载熊猫淘学，专业教师辅导作业！',
                        "description"=>'更多家长推荐使用的App',
                        "url"=>'https://sj.qq.com/myapp/detail.htm?apkName=com.dm.xmtx',
                        "thumb_url"=>'https://panda-app.oss-cn-hangzhou.aliyuncs.com/avatar/default/default.png',
                    )
                );
                $json = json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+
                $this->requestAPI($json);
            }

            elseif($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ //用户进入客服后马上回复，现在已失效,需要用户先发过消息
                $fromUsername = $postArr['FromUserName'];
                $content = '你好,回复1下载app，回复2客服咨询';
                $data=array(
                    "touser"=>$fromUsername,
                    "msgtype"=>"text",
                    "text"=>array("content"=>$content)
                );
                $json = json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+
                $this->requestAPI($json);
            }


            elseif($postArr['MsgType'] !== 'event'){		//用户发送其他内容,引导加客服
                $fromUsername = $postArr['FromUserName'];   //发送者openid
                $imgurl = "./WechatIMG1.jpeg";			//客服微信二维码,相对路径
                $media_id = $this->getMediaId($imgurl);   			//获取图片消息的media_id
                $data=array(
                    "touser"=>$fromUsername,
                    "msgtype"=>"image",
                    "image"=>array("media_id"=>$media_id)
                );
                $json = json_encode($data,JSON_UNESCAPED_UNICODE);  //php5.4+
                $this->requestAPI($json);
            }

            else{
                exit;
            }

        }else{
            echo "empty";
            exit;
        }
    }

    public function requestAPI($json)
    {
        $access_token = $this->get_accessToken();
        /*
         * POST发送https请求客服接口api
         */
        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
        //以'json'格式发送post的https请求
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($json)){
            curl_setopt($curl, CURLOPT_POSTFIELDS,$json);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
        $output = curl_exec($curl);
        if (curl_errno($curl)) {
            echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl);
        if($output == 0){
            echo 'success';exit;
        }
    }

    public function get_accessToken()
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx7716ca1c7cb51926&secret=7975af113e98d67b17ba3edce426547b'; //替换成自己的小程序id和secret
        @$weixin = file_get_contents($url);
        @$jsondecode = json_decode($weixin);
        @$array = get_object_vars($jsondecode);
        $token = $array['access_token'];
        return $token;
    }


    //获取上传图片的media_id
    public function getMediaId($imgurl)
    {
        $token = $this->get_accessToken();
        $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image";
        // echo $url;
        $ch1 = curl_init();
        $timeout = 10;
        $real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";//自动转成图片文件绝对路径,如果图片发送失败,检查PHP的$_SERVER['DOCUMENT_ROOT'的配置
        // echo $real_path;
        $data = array("media" => new CURLFile("{$real_path}"));//php5.6(含)以上版本使用此方法
        // var_dump($data);
        curl_setopt($ch1, CURLOPT_URL, $url);
        curl_setopt($ch1, CURLOPT_POST, 1);
        curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
        $result = curl_exec($ch1);
        // echo $result;
        curl_close($ch1);
        if ($result) {
            $result = json_decode($result, true);
            return $result['media_id'];
        } else {
            return null;
        }
    }
}

